package myJs.api

import japgolly.scalajs.react.extra.Ajax
import myJs.tool.Tool
import shared.pojo.App._
import shared.pojo.Mission.MissionInfo
import upickle.default.read
import upickle.default._

/** Created by yz on 10/10/2023
  */
object Mission {

  val baseUrl = s"${Api.apiUrl}/mission"

  def all = {
    val url = s"${baseUrl}/getAllMission"
    Ajax("GET", s"${url}").setRequestContentTypeJson.send.asAsyncCallback
      .map { xhr =>
        read[List[ujson.Obj]](xhr.responseText)
      }
  }

  def all(request: IdsData) = {
    val url = s"${baseUrl}/all"
    Ajax("POST", s"${url}").setRequestContentTypeJson
      .send(write(request))
      .asAsyncCallback
      .map { xhr =>
        read[List[MissionInfo]](xhr.responseText)
      }
  }

  def deletes(request: IdsData) = {
    val url = s"${baseUrl}/deleteMissionByIds"
    Ajax("POST", s"${url}").setRequestContentTypeJson
      .send(write(request))
      .asAsyncCallback
      .map { xhr =>
        xhr.responseText
      }
  }

  def delete(missionId: Int) = {
    val url = s"${baseUrl}/deleteMissionById?missionId=${missionId}"
    Ajax("GET", s"${url}").setRequestContentTypeJson.send.asAsyncCallback
      .map { xhr =>
        xhr.responseText
      }
  }

  def getById(missionId: Int) = {
    val url = s"${baseUrl}/getById?missionId=${missionId}"
    Ajax("GET", s"${url}").setRequestContentTypeJson.send.asAsyncCallback
      .map { xhr =>
        read[MissionInfo](xhr.responseText)
      }
  }

}
